<?php
/**
 * @author Nguyen Thanh Tung
 * @copyright 2014
 * @description: save action sample.
 */
// not perm edit
if(!isset($AppUI->id)||!isset($_SESSION['user'])||(isset($_SESSION['user'])&&($_SESSION['user']&1<<2)==0)){
    redirect(HOST_BACKEND);
}
$this->load->model('user_model', 'user');
$intUserId = $this->input->post('puserid',0);
$strFirstname        = $this->input->post('pfirstname', null);
$strLastname         = $this->input->post('plastname', null);
$strBirthday         = $this->input->post('pbirthtday', null);
$intGender           = (int)$this->input->post('pgender', 0);
$intStatus           = (int)$this->input->post('pstatus', 0);
$strCountry          = $this->input->post('pcountry', null);
$intRole             = (int)$this->input->post('pprole', 0);
$strAdddress	     = $this->input->post('paddress', null);	
$intDistrict         = $this->input->post('pdistrict', null);	
$intProvince		 = $this->input->post('pprovince', null);	
$strPassport		 = $this->input->post('ppassport', null);	
$strCard_id	     	 = $this->input->post('pcardid', null);	
$arrErr              = array();

// strip tag data input
$strUsername        = CI_Function::stripTag($strUsername);
$lenUsername        = strlen($strUsername);
$lenPassword        = strlen($strPassword);
$strEmailCheck		= CI_Function::isEmail($strEmail);
$strFirstname        = CI_Function::stripTag($strFirstname);
$lenFirstname         = strlen($strFirstname);
$strLastname        = CI_Function::stripTag($strLastname);
$lenLastname         = strlen($strLastname);
// validate manual
	
if(empty($strFirstname))
    array_push($arrErr, $arrLocale['firstname_required']);
elseif(($lenFirstname < 2 || $lenFirstname > VL_USERNAME_MAXLENGTH) ) 
    array_push($arrErr, $arrLocale['firstname_range']);
//+lastname 
if(empty($strLastname))
    array_push($arrErr, $arrLocale['lastname_required']);
elseif(($lenLastname < 2 || $lenLastname > VL_USERNAME_MAXLENGTH) ) 
    array_push($arrErr, $arrLocale['lastname_range']);
   
if(count($arrErr)==0) {//allow save
    // unset session if exists
    if(isset($_SESSION['USER_ERROR'])) unset($_SESSION['USER_ERROR']);
    if(isset($_SESSION['USER_POST'])) unset($_SESSION['USER_POST']);

  
    $intError = $this->user->update($AppUI->id,$intUserId,$strFirstname,$strLastname,CI_Function::convertStrToDate($strBirthday),$intGender,$strAdddress,$intDistrict,$intProvince,$strCountry,$strPassport,$strCard_id);

			
    if($intError==0) CI_Function::setMsg($arrMessage['save_update']);
    elseif($intError==1) array_push($arrErr, $arrLocale['username_validate']);
    elseif($intError==2) array_push($arrErr, $arrLocale['email_match']);        
    elseif($intError==3) array_push($arrErr, $arrLocale['username_validate']);        
}
// occur error

if (isset($arrErr) && count($arrErr) > 0) {
    $_SESSION['USER_ERROR']    = $arrErr;    
    $_SESSION['USER_POST']     = $this->input->post();
    
    redirect(HOST_BACKEND . 'user/edit='.$intUserId);
}
redirect(HOST_BACKEND.'user');
